﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Common.Utils;
using Telerik.WebControls;

public partial class sysadmin_SystemInfo_EditorFellow : PowerCreator.LMS.Web.Base.BasePage
{
    Course opCourse = new Course();
    PowerCreator.LMS.Course.DB.TFellowManager objfellowmanager = new PowerCreator.LMS.Course.DB.TFellowManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            int str_fellowid = 0;

            if (!string.IsNullOrEmpty(Request.Params["FellowID"]))
            {
                str_fellowid = int.Parse(Request.Params["FellowID"].ToString());
                ViewState["FellowID"] = str_fellowid;
            }

            PageLoad(str_fellowid);
        }
    }

    private void PageLoad(int FellowID)
    {

        TFellow vofellow = objfellowmanager.Load(FellowID);

        txt_CourseName.Text = vofellow.FellowName;
        txt_fellowaddress.Text = vofellow.FellowAddress;
        txt_pic.Text = vofellow.FellowIMGUrl;

    }


    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("AddFellow_msg_9", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseName.Text = GetLanageValueByKey("AddFellow_msg_2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Url.Text = GetLanageValueByKey("AddFellow_msg_6", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseImgUrl.Text = GetLanageValueByKey("AddFellow_msg_8", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_msg_description.Text = GetLanageValueByKey("AddCourseInfo_lbl_l1", PowerCreator.LMS.Language.LanagetItemType.Page);

        rfv_CourseName.ErrorMessage = GetLanageValueByKey("AddFellow_msg_3", PowerCreator.LMS.Language.LanagetItemType.Page);


        btn_Save.Text = GetLanageValueByKey("btn_AddCourseInfo_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_AddCourseInfo_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Back.Text = GetLanageValueByKey("btn_UploadResource_Back", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private bool CheckPageData()
    {

        if (!string.IsNullOrEmpty(txt_CourseName.Text.Trim()))
        {
            if (objfellowmanager.Count("fellowName='" + txt_CourseName.Text.Trim() + "'") > 0)
            {
                lab_CouseMsg.Text = GetLanageValueByKey("AddFellow_msg_4", PowerCreator.LMS.Language.LanagetItemType.Page);
                return false;
            }
        }
        else
        {
            lab_CouseMsg.Text = GetLanageValueByKey("AddFellow_msg_3", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }

        return true;
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        TFellow vo = new TFellow();
        vo.FellowName = txt_CourseName.Text.ToString();
        vo.FellowAddress = txt_fellowaddress.Text.Trim();
        vo.FellowIMGUrl = txt_pic.Text.ToString();

        if (objfellowmanager.Update(vo))
        {
            string url = "FellowManager.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "FellowManager.aspx";
        Script.RedirectURL(url);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        //btn_Back.Enabled = false;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/FellowPic/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            txt_pic.Text = Url;
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "gif|jpg";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }
}
